package com.project.service.impl;

import com.project.bean.CutPageBean;
import com.project.bean.FacilityBean;
import com.project.mapper.IFacilityMapper;
import com.project.mapper.IRoomMapper;
import com.project.service.IFacilityService;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class FacilityServiceImpl extends BaseService implements IFacilityService {
    @Override
    public void add(FacilityBean facility, Integer roomId) {
        SqlSession session = this.getSession();
        IFacilityMapper mapper = session.getMapper(IFacilityMapper.class);
        mapper.add(facility,roomId);
        session.commit();
        session.close();
    }

    @Override
    public void press(Integer facilityId, Integer roomId) {
        SqlSession session = this.getSession();
        IFacilityMapper mapper = session.getMapper(IFacilityMapper.class);
        mapper.press(facilityId,roomId);
        if (mapper.getTotalByItem(roomId,"未处理")<=0){
            session.getMapper(IRoomMapper.class).updateStatus(roomId);
        }
        session.commit();
        session.close();
    }

    @Override
    public CutPageBean<FacilityBean> cutByRoomId(Integer pageNO, Integer roomId) {
        CutPageBean<FacilityBean> cutPageBean = new CutPageBean<>();
        SqlSession session = this.getSession();
        IFacilityMapper mapper = session.getMapper(IFacilityMapper.class);
        List<FacilityBean> list = mapper.cutByItemList((pageNO * CutPageBean.PAGESIZE), CutPageBean.PAGESIZE, roomId,null);
        cutPageBean.setList(list);
        cutPageBean.setCount(mapper.getTotalByItem(roomId,null));

        return cutPageBean;
    }
}
